<template>
	<view class="w-100 bg-white">
		<view class="mx-3 border-bottom flex-1 row-ac-sb pt-3 pb-3">
			<view class="font-s-5">账户与安全</view>
			<view class="iconfont icon-next-01 font-s-4 text-light-grey"></view>
		</view>
		<view class="mx-3 border-bottom flex-1 row-ac-sb pt-3 pb-3" @click="toUserInfo">
			<view class="font-s-5">个人设置</view>
			<view class="iconfont icon-next-01 font-s-4 text-light-grey"></view>
		</view>
		<view class="flex-1" style="height: 20rpx;background-color: #f5f5f5;"></view>
		<view class="mx-3 border-bottom flex-1 row-ac-sb pt-3 pb-3">
			<view class="row-ac font-s-5">缓存<text class="ml-4 font-s-3 text-light-grey">{{ currentSize }}</text></view>
			<view class="row-ac text-light-grey" @click="clearStorage">
				<view class="font-s-4 mr-1">清除缓存</view>
			</view>
		</view>
		<view class="mx-3 border-bottom flex-1 row-ac-sb pt-3 pb-3" @click="getAppInfo(1)">
			<view class="font-s-5">版本号{{ appVersion }}</view>
			<view class="row-ac text-light-grey">
				<view class="font-s-4 mr-1" >检查更新</view>
			</view>
		</view>
		<!-- <view class="mt-5 row" @click="logout">
			<view class="row font-s-5 rounded border" style="width: 360rpx;height: 90rpx;">退出登陆</view>
		</view> -->
	</view>
</template>

<script>
	import {
		logout
	} from '@/api/index';
	import {
		mapState
	} from 'vuex';
	import {
		appversionDetailUrl
	} from '@/api/index';
	import {
		checkUpdate
	} from "@/components/update/js/app-update-check.js";
	export default {
		data() {
			return {
				appVersion: '',
				currentSize: ''
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			init() {
				// #ifdef APP-PLUS
				this.getAppVersion()
				this.getStorageSize()
				// #endif

			},
			//获取本地缓存大小
			// #ifdef APP-PLUS
			getStorageSize() {
				let that = this;
				plus.cache.calculate(function(size) {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						that.currentSize = "0B";
					} else if (sizeCache < 1024) {
						that.currentSize = sizeCache + "B";
					} else if (sizeCache < 1048576) {
						that.currentSize = (sizeCache / 1024).toFixed(2) + "KB";
					} else if (sizeCache < 1073741824) {
						that.currentSize = (sizeCache / 1048576).toFixed(2) + "MB";
					} else {
						that.currentSize = (sizeCache / 1073741824).toFixed(2) + "GB";
					}
					console.log(that.currentSize);
				});
			},
			// #endif
			// 清理缓存
			clearStorage() {
				let that = this;
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					console.log(files, len)
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录
									that.files = []
									uni.showToast({
										title: '清除成功',
										duration: 2000
									});
									that.getStorageSize(); // 重新计算缓存  
								}, function(e) {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							console.log('文件路径读取失败')
						});
					}
				} else { // ios  
					plus.cache.clear(function() {
						uni.showToast({
							title: '清除成功',
							duration: 2000
						});
						that.getStorageSize();
					});
				}
			},
			// #ifdef APP-PLUS
			getAppVersion() {
				plus.runtime.getProperty(plus.runtime.appid, (res) => {
					this.appVersion = res.version
				})
			},
			// #endif
			// #ifdef APP-PLUS
			getAppInfo(type) {
				const system_info = uni.getSystemInfoSync();
				let params = {
					os: system_info.platform //本机设备操作系统  （android || ios） 
				}
				if (params.os != 'ios' && params.os != 'android') false; //如果不是安卓或ios 返回false
				plus.runtime.getProperty(plus.runtime.appid, (res) => {
					this.appVersion = res.version
				})
				//请求获取最新版本
				setTimeout(() => {
					this.$R.get(appversionDetailUrl + 1 + '/').then(req => {
						this.version = req.data.version
						let update_info = {
							version: req.data.version, //线上版本
							now_url: req.data.app_url, //更新链接
							silent: 0, //是否是静默更新
							force: req.data.force, //是否是强制更新
							net_check: 1, //非WIfi是否提示
							note: req.data.note, //更新内容
						}
						checkUpdate(update_info, type).then(res => {
							if (res.msg) {
								plus.nativeUI.toast(res.msg);
							}
						});
					}, 200)
				})
				if(this.appVersion == this.version){
					uni.showToast({
						title: '已经是最新版本'
					});
				}
			},
			// #endif
			logout() {
				uni.showModal({
					content: '是否要退出登陆',
					success: res => {
						if (res.confirm) {
							// 从缓存中移除用户信息
							uni.removeStorageSync('token');
							uni.removeStorageSync('userInfo');
							uni.removeStorageSync('distributionUserCenterInfo');
							uni.removeStorageSync('distributionUserInfo');
							uni.removeStorageSync('defaultAddress');
							uni.removeStorageSync('weChatOpenId');
							this.$store.commit('setUserToken', '')
							// vuex中的收货地址也清空

							uni.navigateBack({
								delta: 1
							});
							uni.showToast({
								title: '退出登陆成功'
							});
						}
					}
				});
			},
			toSet(url) {
				uni.navigateTo({
					url
				});
			},
			toUserInfo() {
				uni.navigateTo({
					url: '/pages/user/settings/userset'
				});
			}
		}

	}
</script>

<style lang="scss">
	.border-bottom {
		border-bottom-color: #f5f5f5;
	}
</style>